package com.flash.BusinessParameters.controller;

import com.flash.BusinessParameters.entity.Cash;
import com.flash.BusinessParameters.service.CashService;
import com.flash.GlobalManagement.util.Dbutil;
import com.flash.GlobalManagement.util.TableIdUtil;
import com.flash.GlobalManagement.util.TableNameUtil;
import com.flash.GlobalManagement.util.TableTimeUtil;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CashController {
    @Autowired
    CashService cashService;

    @Resource
    Dbutil dbutil;

    @RequestMapping("/insertCash")
    @ResponseBody
    public String insertCash(Cash cash){
        System.out.println("添加用户");
      // cash.setBearingTime(Integer.parseInt(cash.getBearingTimes()));
      // cash.setStockpileType(Integer.parseInt(cash.getStockpileTypes()));
        cash.setCashId(dbutil.getMaxId3(TableIdUtil.CID, TableNameUtil.C, TableTimeUtil.CTime,cash.getEndTime()));
        int i = cashService.insertCash(cash);
        if (i>0){
            return "ok";
        }else{
            return "no";
        }
    }

    @RequestMapping("/deleteCash")
    @ResponseBody
    public int deleteCash(String cashId){
        System.out.println("删除用户");
        int i = cashService.deleteCash(cashId);
        System.out.println(i);
        return i;
    }

    @RequestMapping("/selectCash")
    @ResponseBody
    public Map selectCash(String accountName , int page, int limit, HttpServletRequest request){
        System.out.println("查询");
        HashMap map = new HashMap();
        System.out.println(accountName);
        HttpSession session = request.getSession();
        session.setAttribute("accountName",accountName);
        List<Cash> cash = cashService.selectCashMap(accountName, page, limit);
        for (int i = 0; i < cash.size(); i++) {
            Cash cash1 = cash.get(i);
            if (cash1.getStockpileType()==1){
                cash1.setStockpileTypes("活期");
            }else {
                cash1.setStockpileTypes("定期");
            }

            if (cash1.getBearingTime()==1){
                cash1.setBearingTimes("360天");
            } else if (cash1.getBearingTime()==2) {
                cash1.setBearingTimes("365天");
            }else {
                cash1.setBearingTimes("366天");
            }
        }
        int count = cashService.selectCountCash();
        System.out.println(cash);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",cash);
        return map;
    }

    @RequestMapping("/updateCash")
    @ResponseBody
    public int updateCash(Cash cash){
        System.out.println(cash);
       // cash.setBearingTime(Integer.parseInt(cash.getBearingTimes()));
        //cash.setStockpileType(Integer.parseInt(cash.getStockpileTypes()));
        System.out.println("修改");
        int i = cashService.updateCash(cash);
        System.out.println(i);
        return i;
    }

    @RequestMapping("/selectTable")
    @ResponseBody
    public String select(){
        String maxId = dbutil.getMaxId("cashId", "cash","openTime");
        return maxId;
    }
}
